--[[
	Copyright (C) Udorn (Blackhand)
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.	
--]]

--[[
	Widget for editing the buyout multiplier for auctions.
--]]
vendor.BuyoutModifier = {}
vendor.BuyoutModifier.prototype = {}
vendor.BuyoutModifier.metatable = {__index = vendor.BuyoutModifier.prototype}

local L = vendor.Locale.GetInstance()
local log = vendor.Debug:new("BuyoutModifier")

local PERCENT_TYPE = 1
local MINUS_TYPE = 2
local PLUS_TYPE = 3

--[[
	Sets the value according to the modifier type.
--]]
local function _SetValue(self, modType, val)
	log:Debug("_SetValue modType: %d val: %d", modType, val or 0)
	if (modType == PERCENT_TYPE) then
		self.percentBut:SetText(val or 0)
		self.absBut:Hide()
		self.percentBut:Show()
	elseif (modType == MINUS_TYPE) then
		MoneyInputFrame_SetCopper(self.absBut, val or 0)
		self.absBut:Show()
		self.percentBut:Hide()
	elseif (modType == PLUS_TYPE) then
		MoneyInputFrame_SetCopper(self.absBut, val or 0)
		self.absBut:Show()
		self.percentBut:Hide()
	end
end

--[[
	Percentage has been selected.
--]]
local function _OnEditButtonUpdate(self)
	local modType = self.buyoutMod[self.priceModel].modType
	if (modType == PERCENT_TYPE) then
		self.buyoutMod[self.priceModel].percent = self.percentBut:GetNumber()
		log:Debug("_OnEditButtonUpdate priceModel: %d percent: %d", self.priceModel, self.buyoutMod[self.priceModel].percent)
		vendor.Seller:RefreshPrices()
	end
end

--[[
	Absolute value has been updated
--]]
local function _OnAbsButtonUpdated(self)
	local modType = self.buyoutMod[self.priceModel].modType	
	if (modType == MINUS_TYPE) then
		self.buyoutMod[self.priceModel].minus = MoneyInputFrame_GetCopper(self.absBut)
		log:Debug("_OnAbsButtonUpdated new minus: %d", self.buyoutMod[self.priceModel].minus)
	elseif (modType == PLUS_TYPE) then
		self.buyoutMod[self.priceModel].plus = MoneyInputFrame_GetCopper(self.absBut)
		log:Debug("_OnAbsButtonUpdated new plus: %d", self.buyoutMod[self.priceModel].plus)
	end
	vendor.Seller:RefreshPrices() 
end

--[[
	Initializes the frame.
--]]
local function _Init(self, parentFrame)
	local frame = CreateFrame("Frame", nil, parentFrame)
	frame:SetWidth(150)
	frame:SetHeight(60)
	frame:Hide()
	
	local percentBut = vendor.GuiTools.CreateWidget("EditBox", "SalesFrameBuyoutMulEdit", frame, "InputBoxTemplate", 110, 16, L["Percentage to be multiplied with the buyout price."])
	percentBut:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -10)
	percentBut.obj = self
	percentBut:SetNumeric(true)
	percentBut:SetAutoFocus(false)
	percentBut:SetScript("OnTextChanged", function(but) _OnEditButtonUpdate(but.obj) end)
	
	local absBut = CreateFrame("Frame", "SalesFrameBuyoutMulAbs", frame, "MoneyInputFrameTemplate")
	absBut:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -10)
	local obj = self -- no argument possible
	MoneyInputFrame_SetOnValueChangedFunc(absBut, function() _OnAbsButtonUpdated(obj) end)
	SalesFrameBuyoutMulAbsGold:SetMaxLetters(2)
	SalesFrameBuyoutMulAbsGold:SetWidth(20)
	absBut:SetScale(0.9)
	absBut:Hide()
	
	local f = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
	f:SetText(L["Price modifier"])
	f:SetPoint("BOTTOMLEFT", SalesFrameBuyoutMulEdit, "TOPLEFT", -4, 4)
	
	local modType = vendor.DropDownButton:new("SalesFrameBuyoutMulType", frame, 35, nil, L["Selects the type of price modification."]);
	modType:SetPoint("LEFT", SalesFrameBuyoutMulEdit, "RIGHT", -9, -2)
 	modType:SetId(1)
	modType:SetListener(self)
    local types = {}
	table.insert(types, {value = PERCENT_TYPE, text = "\%"})
	table.insert(types, {value = MINUS_TYPE, text = "-"})
	table.insert(types, {value = PLUS_TYPE, text = "+"})
	modType:SetItems(types, PERCENT_TYPE)

	self.frame = frame
	self.percentBut = percentBut
	self.absBut = absBut
	self.modType = modType
end

--[[ 
	Creates a new instance.
--]]
function vendor.BuyoutModifier:new(parentFrame, buyoutMod)
	local instance = setmetatable({}, self.metatable)
	_Init(instance, parentFrame)
	instance.buyoutMod = buyoutMod
	return instance
end

--[[
	Shows the frame
--]]
function vendor.BuyoutModifier.prototype:Show()
	self.frame:Show()
end

--[[
	Hides the frame
--]]
function vendor.BuyoutModifier.prototype:Hide()
	self.frame:Hide()
end

--[[
	Selects the setting for the given price model.
--]]
function vendor.BuyoutModifier.prototype:SelectPriceModel(priceModel)
	self.priceModel = priceModel
	local modType = self.buyoutMod[self.priceModel].modType
	log:Debug("SelectPriceModel priceModel: %d modType: %d", priceModel, modType)
	if (modType == PERCENT_TYPE) then
		_SetValue(self, modType, self.buyoutMod[self.priceModel].percent)
		self.modType:SelectValue(modType)
	elseif (modType == MINUS_TYPE) then
		_SetValue(self, modType, self.buyoutMod[self.priceModel].minus)
		self.modType:SelectValue(modType)
	elseif (modType == PLUS_TYPE) then
		_SetValue(self, modType, self.buyoutMod[self.priceModel].plus)
		self.modType:SelectValue(modType)
	end
end

--[[
	Returns an altered buyout according to the current settings.
--]]
function vendor.BuyoutModifier.prototype:ModifyBuyout(buyout)
	local modType = self.buyoutMod[self.priceModel].modType
	if (modType == PERCENT_TYPE) then
		local mul = self.buyoutMod[self.priceModel].percent
		buyout = math.floor(buyout * (mul / 100.0) + 0.5);
	elseif (modType == MINUS_TYPE) then
		log:Debug("ModifyBuyout minus: %d", self.buyoutMod[self.priceModel].minus or 0)
		buyout = buyout - (self.buyoutMod[self.priceModel].minus or 0)
	elseif (modType == PLUS_TYPE) then
		log:Debug("ModifyBuyout plus: %d", self.buyoutMod[self.priceModel].plus or 0)
		buyout = buyout + (self.buyoutMod[self.priceModel].plus or 0)
	end 
	buyout = math.max(1, buyout)
	return buyout
end

--[[
	Sets the postion of the frame.
--]]
function vendor.BuyoutModifier.prototype:SetPoint(...)
	self.frame:SetPoint(...)
end

--[[
	Interface method for DropDownButton.
--]]
function vendor.BuyoutModifier.prototype:DropDownButtonSelected(button, value)
	if (value == PERCENT_TYPE) then
		self.buyoutMod[self.priceModel].modType = PERCENT_TYPE
		_SetValue(self, value, self.buyoutMod[self.priceModel].percent)
	elseif (value == MINUS_TYPE) then
		self.buyoutMod[self.priceModel].modType = MINUS_TYPE
		_SetValue(self, value, self.buyoutMod[self.priceModel].minus)
	elseif (value == PLUS_TYPE) then
		self.buyoutMod[self.priceModel].modType = PLUS_TYPE
		_SetValue(self, value, self.buyoutMod[self.priceModel].plus)
	end
	-- refresh
	vendor.Seller:RefreshPrices()
end
